﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThietBiSo.LinQDB;


namespace ThietBiSo.Models
{
    public class MSanPham
    {
        private _24hmuaonlineDataContext context = new _24hmuaonlineDataContext();
        public List<SanPham> FindAll()
        {
            var v = from f in context.SanPhams orderby f.ThuTu ascending select f;
            return v.ToList();
        }
        public List<SanPham> FindAll_Web()
        {
            var v = from f in context.SanPhams.Where(d=>d.TrangChu==true) orderby f.ThuTu ascending select f;
            return v.ToList();
        }
        public List<SanPham> GetListSPByMaDM(int ma)
        {
            var v = from f in context.SanPhams.Where(d => d.MaDM == ma && d.HienThi == true) select f;
            return v.ToList();
        }
       
        public SanPham ChiTiet(int id)
        {
            var v = from f in context.SanPhams.Where(d => d.MaSP == id) select f;
            return v.SingleOrDefault();
        }
        public string GetTitleByMa(int ma)
        {
            var v = from f in context.SanPhams.Where(d => d.MaSP == ma) select f.TenSP;
            return v.FirstOrDefault();
        }
        public int GetTotal()
        {
            return FindAll().Count();
        }
        public int GetTotal(string madm)
        {
            int total = 0;
            if (madm == "" || madm == "0")
            {
                total = (from f in context.SanPhams select f).Count();

            }
            else
            {
                total = (from f in context.SanPhams.Where(d => d.MaDM == int.Parse(madm)) select f).Count();
            }

            return total;

           // return (from f in context.SanPhams.Where(d => d.MaDM == int.Parse(madm)) select f).Count();
        }
        public void Save(SanPham lsp)
        {
            context.SanPhams.InsertOnSubmit(lsp);
            context.SubmitChanges();
        }
        public void Update(SanPham lsp)
        {
            var v = from f in context.SanPhams
                    select f;
            SanPham lspdb = v.SingleOrDefault(d => d.MaSP.Equals(lsp.MaSP));
            lspdb.TenSP = lsp.TenSP;
            lspdb.HinhAnh = lsp.HinhAnh;
            lspdb.ChiTiet = lsp.ChiTiet;
            lspdb.MaDM = lsp.MaDM;
            lspdb.ThuTu = lsp.ThuTu;
            context.SubmitChanges();
        }

        public string Delete(int ma)
        {
            try
            {
              
                var v = from p in context.SanPhams
                        where p.MaSP.Equals(ma)
                        select p;
                SanPham spdb = v.SingleOrDefault();
                context.SanPhams.DeleteOnSubmit(spdb);
                context.SubmitChanges();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        public string HienThi(int id)
        {
            try
            {
                var v = from f in context.SanPhams
                        select f;
                SanPham lspdb = v.SingleOrDefault(d => d.MaSP.Equals(id));
                if (lspdb.HienThi == true)
                {
                    lspdb.HienThi = false;
                }
                else lspdb.HienThi = true;
                context.SubmitChanges();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        public string TrangChu(int id)
        {
            try
            {
                var v = from f in context.SanPhams
                        select f;
                SanPham lspdb = v.SingleOrDefault(d => d.MaSP.Equals(id));
                if (lspdb.TrangChu == true)
                {
                    lspdb.TrangChu = false;
                }
                else lspdb.TrangChu = true;
                context.SubmitChanges();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        public void SaveChange()
        {
        }
        public SanPham GetByID(int id)
        {
            return (FindAll().Where(d => d.MaSP == id)).SingleOrDefault();
        }

        public List<SanPham> GetList(int start, int offset)
        {
            var v = new List<SanPham>();

            v = FindAll().Skip(start).Take(offset).ToList();

            return v;
        }
        public List<SanPham> GetList(int start, int offset,string madm)
        {
            var v = new List<SanPham>();

            v = (from f in context.SanPhams.Where(d => d.MaDM == int.Parse(madm)) select f).Skip(start).Take(offset).ToList();

            return v;
        }
        public int GetMax()
        {
            return (from t in context.SanPhams
                    orderby t.MaSP descending
                    select t.MaSP).FirstOrDefault();
        }
        public int? GetMaxTT()
        {
            return (from t in context.SanPhams
                    orderby t.ThuTu descending
                    select t.ThuTu).FirstOrDefault();
        }
    }
}